package se.webbzon.oschi01.gui;

import java.util.Observable;

import se.webbzon.oschi01.command.History;
import se.webbzon.oschi01.command.UndoableClipboard;
import se.webbzon.oschi01.gui.MainModelEvent.Action;
import se.webbzon.oschi01.world.Chunk;
import se.webbzon.oschi01.world.Dimension;
import se.webbzon.oschi01.world.Region;
import se.webbzon.oschi01.world.World;

public class MainModel extends Observable {
	
	private History history;
	
	private UndoableClipboard clipboard;
	
	private World world;
	
	private Dimension dimension;
	
	private Region region;
	
	private Chunk chunk;
	
	private String datum;
	
	public MainModel(History history, UndoableClipboard clipboard) {
		this.history = history;
		this.clipboard = clipboard;
	}
	
	public History getHistory() {
		return history;
	}
	
	public UndoableClipboard getClipboard() {
		return clipboard;
	}

	public World getWorld() {
		return world;
	}

	public void setWorld(World world) {
		this.world = world;
		dimension = null;
		region = null;
		chunk = null;
		datum = "";
		setChanged();
		notifyObservers(new MainModelEvent(this,Action.WORLD_CHANGED));
	}

	public Dimension getDimension() {
		return dimension;
	}

	public void setDimension(Dimension dimension) {
		if (world != null && world.hasDimension(dimension)) {
			this.dimension = dimension;
			region = null;
			chunk = null;
			setChanged();
			notifyObservers(new MainModelEvent(this,Action.DIMENSION_CHANGED));
		}
	}

	public Region getRegion() {
		return region;
	}

	public void setRegion(Region region) {
		if (dimension != null && dimension.hasRegion(region)) {
			this.region = region;
			chunk = null;
			setChanged();
			notifyObservers(new MainModelEvent(this,Action.REGION_CHANGED));
		}
	}

	public Chunk getChunk() {
		return chunk;
	}

	public void setChunk(Chunk chunk) {
		if (region != null && region.hasChunk(chunk)) {
			this.chunk = chunk;
			setChanged();
			notifyObservers(new MainModelEvent(this,Action.CHUNK_CHANGED));
		}
	}
	
	public String getDatum() {
		return datum;
	}
	
	public void setDatum(String datum) {
		if (world != null && world.hasDatum(datum)) {
			this.datum = datum;
			setChanged();
			notifyObservers(new MainModelEvent(this,Action.CHUNK_CHANGED));
		}
	}

}
